<style scoped>
    .layui-row {
        overflow: hidden;
    }
    .select-item {
        width: 34.333333%;
        float: right;
    }
    .layui-row:after, .layui-row:before {
        clear: none;
    }
</style>

<template>
    <div class="department">
        <div>
            <a href="javascript:;" class="layui-btn" @click="add"><i class="layui-icon">&#xe654;</i>添加权限信息</a>
        </div>
        <div class="layui-row">
            <div  class="layui-col-lg4 select-item" style="text-align: right">
                <div class="layui-input-inline" style="width: 300px">
                    <input type="text" v-model="searchName" placeholder="用户姓名" class="layui-input key">
                </div>
                <button type="button" class="layui-btn sou" @click="search">搜索</button>
            </div>
        </div>
        <table class="layui-table layui-form">

            <thead>
            <tr>
                <th>部门</th>
                <th>用户</th>
                <th>创建时间</th>
                <th>操作人</th>
                <th>操作</th>

            </tr>
            </thead>
            <tbody>
            <template v-for="(item) in afterList" >
                <tr class="id9"  v-for="(row, i) in item" >
                    <td  v-if="i === 0" :rowspan="item.length">{{row.depname}}</td>
                    <td  >{{row.realname}}</td>
                    <td>{{row.createtime}}</td>
                    <td>{{row.operatorName}}</td>
                    <td><a href="javascript:;" @click="del(row.guid)">删除</a></td>
                </tr>
            </template>
            </tbody>
        </table>

        <Pagination :cur="currentPage" :all="totalPage" :callback="turnPage" ></Pagination>
    </div>
</template>

<script>
    import Pagination from '../common/Pagination.vue'
    export default {
        name: 'UserLinkDepaList',
        components: {Pagination},
        data () {
            return {
                reldepuser: [],
                currentPage: 1,
                rows: 10,
                totalPage: 0,
                searchName: '',
                afterList: []
            }
        },
        mounted() {
            let that = this;
            layui.use(['form'], function () {
                var form = layui.form,
                    layer = layui.layer;
                    that.getPageData();
            });
        },
        methods: {
            // 数据转换
            tansList () {
                this.afterList = Object.values(this.reldepuser.reduce((temp, item) => {
                    if( temp[item.telphone] ) {
                        temp[item.telphone].push(item)
                    }else {
                        temp[item.telphone]  = [item]
                    }
                    return temp
                },{}))
            },
            search: function() {
                this.getPageData('search');
            },
            turnPage: function(data){
                this.currentPage = data;
                this.getPageData();
            },
            getPageData: function(type = '') {
                let that = this;
                let loader = layui.layer.load();

                let postData = {rows: that.rows};
                if (type != 'search') {
                    postData.page = that.currentPage;
                }
                postData.realname = that.searchName;

                $.ajax({
                    url: '/produce/Reldepuser!list.action',
                    type: "post",
                    data: postData,
                    dataType: "json",
                    success: function(res) {

                        if (res.info != undefined && res.info.rows != undefined) {
                            //分页数据赋值
                            that.currentPage = res.info.indexPage;
                            that.totalPage = res.info.pageCount;
                            that.reldepuser = res.info.rows;
                            that.tansList()

                        } else {
                            that.alert("系统错误");
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });
            },
            compare: function (property){
                return function(a,b){
                    var value1 = a[property];
                    var value2 = b[property];
                    return value1 - value2;
                }
            },
            add: function () {
                this.$parent.switchPage('AddUserLinkDepa');
            },
            edit: function (id) {
                this.$parent.gid = id;
                this.$parent.switchPage('EditDepartment');
            },
            del: function (id) {
                let that = this;
                layer.confirm('确定要删除该数据吗？', function () {
                    let loader = layer.load();
                    $.ajax({
                        url: '/produce/Reldepuser!del.action',
                        type: "post",
                        data: {guid: id},
                        dataType: "json",
                        success: function(res) {
                            if (res.info == "ok") {
                                that.toast("删除成功");
                                that.getPageData('search');
                            } else {
                                that.toast("系统错误");
                            }
                        },
                        complete: function () {
                            layer.close(loader);
                        }
                    });
                })
            }
        }
    }
</script>
